<?php
namespace app\shop\controller;

use think\Controller;

class Cart extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->TypeModel = model("Product.Type");
        $this->ProductModel = model("Product.Product");
        $this->CartModel = model("Product.Cart");
        $this->BusinessModel = model("Business.Business");
        $this->business = [];
        if ($this->request->isPost()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $this->business = $this->BusinessModel->find($busid);
            if (!$this->business) {
                $this->error('请先登录');
                exit;
            }
        }
    }
    public function index()
    {
        if ($this->request->isPost()) {
            $cartids=$this->request->param('cartids',0,'trim');
            $where=[
                'busid'=>$this->business->id,
                
            ];
            if($cartids)
            {
                $where['cart.id']=['IN',$cartids];
            }

            $cart = $this->CartModel->with(['product'])->where($where)->select();

            if ($cart) {
                $this->success('购物车数据', null, $cart);
                exit;
            } else {
                $this->error('您暂未添加购物车');
                exit;
            }
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $proid = $this->request->param('proid', 0, 'trim');
            $product = $this->ProductModel->with(['unit', 'type'])->find($proid);

            // 判断商品是否存在
            if (!$product) {
                $this->error('商品不存在');
                exit;
            }

            // 判断条件
            $where = [
                'busid' => $this->business->id,
                'proid' => $proid
            ];

            // 查询购物车
            $cart = $this->CartModel->where($where)->find();
            if (!$cart) {
                // 插入购物车
                // 组装数据
                $data = [
                    'proid' => $proid,
                    'busid' => $this->business->id,
                    'nums' => 1,
                    'price' => $product['price'],
                    'total' => $product['price']
                ];
                $result = $this->CartModel->validate('common/Product/Cart')->save($data);
                if ($result === false) {
                    $this->error($this->CartModel->getError());
                    exit;
                } else {
                    $this->success('添加购物车成功',null,$result);
                    exit;
                }

            } else {
                // 更新购物车
                // 增加一个数量
                $nums = bcadd($cart['nums'], 1);

                // 总价
                $total = bcmul($nums, $cart['price']);

                // 组装数据
                $data = [
                    'id' => $cart['id'],
                    'nums' => $nums,
                    'total' => $total,
                ];

                $result = $this->CartModel->isUpdate(true)->save($data);
                if ($result === false) {
                    $this->error($this->CartModel->getError());
                    exit;
                } else {
                    $this->success('添加购物车成功');
                    exit;
                }

            }
        }
    }

    // 数量改变
    public function edit()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', 0, 'trim');
            $nums = $this->request->param('nums', 1, 'trim');
            $where=[
                'busid'=>$this->business->id,
                'id'=>$id
            ];
            $cart = $this->CartModel->where($where)->find($id);
            // var_dump($nums);
            // exit;
            // 判断商品是否存在
            if (!$cart) {
                $this->error('购物车不存在');
                exit;
            }

            // 总价
            $total = bcmul($nums, $cart['price']);

            // 组装数据
            $data = [
                'id' => $cart['id'],
                'nums' => $nums,
                'total' => $total,
            ];
            // halt($data);
            // exit;

            $result = $this->CartModel->isUpdate(true)->save($data);
            if ($result === false) {
                $this->error($this->CartModel->getError());
                exit;
            } else {
                $this->success('更新购物车成功');
                exit;
            }


        }
    }

    // 删除购物车
    public function del()
    {
        if ($this->request->isPost())
        {
            $id = $this->request->param('id', 0, 'trim');
            $cart=$this->CartModel->find($id);
            if(!$cart)
            {
                $this->error('该购物车不存在');
                exit;
            }
            $result=$this->CartModel->destroy($id);
            if($result===false)
            {
                $this->error($this->CartModel->getError());
                exit;
            }
            $this->success('删除购物车成功');
            exit;
        }
    }
}